package com.loto.webapp.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.loto.webapp.common.PageResultSet;
import com.loto.webapp.dao.BaseDao;
import com.loto.webapp.dao.UserDao;
import com.loto.webapp.model.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
	private BaseDao baseDao;

	public void save(User user) {
		getHibernateTemplate().save(user);
	}

	public void update(User user) {
		getHibernateTemplate().update(user);
	}

	public void delete(User user) {
		getHibernateTemplate().delete(user);
	}

	public User findByUsername(String username) {
		List list = getHibernateTemplate().find(
				"FROM com.loto.webapp.model.User WHERE USER_NAME=?", username);
		return list.isEmpty() ? null : (User) list.get(0);
	}

	public PageResultSet<Object> getUserList(int pageSize, int pageIndex) {
		String sql = "FROM com.loto.webapp.model.User ORDER BY userLastActiveOn DESC";
		return baseDao.queryByPage(sql, pageSize, pageIndex);
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public PageResultSet<Object> getUserListByUsername(String username,
			int pageSize, int pageIndex) {
		String sql = "FROM com.loto.webapp.model.User WHERE userName like '%"
				+ username + "%' ORDER BY userLastActiveOn DESC";
		return baseDao.queryByPage(sql, pageSize, pageIndex);
	}

}
